package com.zxl.yjb.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.HorizontalImageAdapter;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.FileUploadBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.tv_ok)
    TextView tv_ok;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.iv_add_pic)
    ImageView ivAddPic;
    @BindView(R.id.ll_add_pic)
    LinearLayout llAddPic;
    @BindView(R.id.recycler_pic_list)
    RecyclerView recyclerPicList;

    private DataManager dataManager;
    private String imgs = "";
    private String remark = "";
    private String phone = "";

    private List<LocalMedia> selectList = new ArrayList<>();//选择图片路径
    private HorizontalImageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        tvPageTitle.setText("意见反馈");

        LinearLayoutManager manager = new LinearLayoutManager(FeedbackActivity.this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerPicList.setLayoutManager(manager);
        HorizontalImageAdapter.onAddPicClickListener onAddPicClickListener = new HorizontalImageAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick(View view) {
                DisplayUtil.openAlbum(FeedbackActivity.this, 8, selectList, PictureConfig.CHOOSE_REQUEST);
            }
        };
        adapter = new HorizontalImageAdapter(FeedbackActivity.this, onAddPicClickListener);
        adapter.setList(selectList);
        adapter.setSelectMax(8);
        recyclerPicList.setAdapter(adapter);
    }


    private void uploadImage()
    {
        dataManager = new DataManager();
        ObserverOnNextListener<FileUploadBean> listener = new ObserverOnNextListener<FileUploadBean>() {
            @Override
            public void onNext(FileUploadBean fileUploadBean) {
                if (fileUploadBean.getUrls().size()>0)
                {
                    for (int i = 0; i < fileUploadBean.getUrls().size(); i++) {
                        imgs+=fileUploadBean.getUrls().get(i)+",";
                    }
                }
                sendData();
            }
        };
        dataManager.uploadMultiPicture(new ProgressObserver<FileUploadBean>(listener,this),selectList);
    }

    private void sendData()
    {
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("remark",remark);
        map.put("picture",imgs);
        map.put("phone",phone);
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                ToastUtils.show(FeedbackActivity.this,"感谢您的反馈，我们将及时处理");
                finish();
            }
        };
        dataManager.suggest_send(new ProgressObserver<Object>(listener,this),map);
    }

    @OnClick({R.id.iv_back, R.id.iv_add_pic,R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_add_pic:
                DisplayUtil.openAlbum(FeedbackActivity.this, 8, selectList, PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.tv_ok:

                remark = etContent.getText().toString();
                phone = etPhone.getText().toString();
                if (TextUtils.isEmpty(phone))
                {
                    ToastUtils.show(FeedbackActivity.this,"请输入联系方式");
                    return;
                }
                if (TextUtils.isEmpty(remark))
                {
                    ToastUtils.show(FeedbackActivity.this,"请输入内容");
                    return;
                }
                if (remark.length()<=10)
                {
                    ToastUtils.show(FeedbackActivity.this,"内容需要大于10个字");
                    return;
                }
                if (selectList.size()>0)
                {
                    uploadImage();
                }
                else
                {
                    sendData();
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST: // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    //刷新图片列表；
                    llAddPic.setVisibility(View.GONE);
                    recyclerPicList.setVisibility(View.VISIBLE);
                    adapter.setList(selectList);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    }
}
